//
//  HMVisttorLoginView.swift
//  sinaweibo
//
//  Created by lst on 16/5/21.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit
import SnapKit


//声明协议 -- 遵循基类
@objc protocol HMVisttorLoginViewDelegate: NSObjectProtocol{
    //声明协议方法 -- 属性
    
    //声明登陆协议方法
    optional func userWillLogin()
    //声明注册协议方法
    func userWillRegister()
}


class HMVisttorLoginView: UIView {
    //MARK: 声明代理属性
    //声明代理属性  swift引用属性都为强引用 代理属性要用弱引用 前加weak
    weak var vistorDelegate: HMVisttorLoginViewDelegate?
    
    //MARK:通过外界设置调用设置访客的页面信息
    func updateUI(title: String, imageName: String, isHome: Bool = false) {
        if isHome{
            startAnimation()
        }else{
            //不是首页 隐藏largeHouse
            largeHouse.hidden = true
            //将圈圈一层置于最长曾
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
    }
    
    
    private func startAnimation(){
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        //基本设置
        //动画时间
        basic.duration = 15
        //动画的重复次数
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        //当动画结束 或者 视图处于非活跃状态 动画效果不移除图层
        basic.removedOnCompletion = false
        //给图层添加动画效果
        circleIcon.layer.addAnimation(basic, forKey: nil)
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(hubImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置最大布局宽度
        tipLabel.preferredMaxLayoutWidth = 230
        
        circleIcon.snp_makeConstraints{(make)in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        hubImage.snp_makeConstraints{(make)in
            //            make.centerX.equalTo(circleIcon.snp_centerX)
            //            make.centerY.equalTo(circleIcon.snp_centerY)
            make.center.equalTo(circleIcon.snp_center)
        }
        largeHouse.snp_makeConstraints{(make)in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints{(make)in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            //约束高度 解决页面跳动的问题
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints{(make)in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints{(make)in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        //一般使用sRGB取值
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //MARK: 添加点击事件
        loginBtn.addTarget(self, action: #selector(HMVisttorLoginView.loginBtnBeClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(HMVisttorLoginView.registorBeClick), forControlEvents: .TouchUpInside)
    }
    
    func loginBtnBeClick(){
        //使用代理属性 调用协议方法
        // '?' 判断代理对象是否为空
        //userWilllogin? 是否响应协议方法
        vistorDelegate?.userWillLogin?()
    }
    
    func registorBeClick(){
        vistorDelegate?.userWillRegister()
    }
    
    
    
    //懒加载子视图
    //添加圈图
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //加载房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //添加遮罩图片
    private lazy var hubImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //
    
    //提示登录注册的"文案"
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", font: 14, textColor: UIColor.darkGrayColor())
//        let lab = UILabel()
//        lab.text = "关注一些人,回到这里看看有什么惊喜"
//        //字号  和颜色
//        lab.textColor = UIColor.darkGrayColor()
//        lab.font = UIFont.systemFontOfSize(14)
//        lab.textAlignment = .Center
//        //设置自动换行
//        lab.numberOfLines = 0
//        //最大的布局宽度
//        lab.preferredMaxLayoutWidth = 230
//
//        //自适应大小
//        lab.sizeToFit()
//        
//        return lab
//        
//    }()
    //登陆注册按钮
    private lazy var loginBtn: UIButton = {
        
        let login = UIButton()
        
        login.setTitle("登陆", forState: .Normal)
        login.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //设置文字大小
        login.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        login.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return login
    }()
    
    private lazy var registerBtn: UIButton = {
        let register = UIButton()
        register.setTitle("注册", forState: .Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        //设置文字大小
        register.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        register.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return register
    }()
    
    
}
